#include <qmath.h>


#include "qparticle.h"

float Q_rsqrt( float number )
{
        long i;
        float x2, y;
        const float threehalfs = 1.5F;

        x2 = number * 0.5F;
        y  = number;
        i  = * ( long * ) &y;                       // evil floating point bit level hacking
        i  = 0x5f3759df - ( i >> 1 );               // what the fuck?
        y  = * ( float * ) &i;
        y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//      y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

        return y;
}

QParticle::QParticle(const QVector2D & position, qreal aMass) :
    Force(0.0, 0.0),  Position(position)
{
    setMass(aMass);
}

QParticle::QParticle(qreal aMass) :
    Force(0.0, 0.0),  Position(0.0, 0.0)
{
    setMass(aMass);
}

void QParticle::setMass(qreal massInKg)
{
    if (massInKg == 0)
    {
        InvertMass = 0;
    }
    else
    {
        InvertMass = 1.0f / massInKg;
    }
}

bool QParticle::zeroVelocity(bool dontNull)
{
    if(Velocity.isNull())
    {
        return true;
    }

    if((qAbs(Velocity.x()) < KZEROVELOCITY) &&
       (qAbs(Velocity.y()) < KZEROVELOCITY))
    {
        if(!dontNull)
        {
            Velocity.setX(0.0);
            Velocity.setY(0.0);
        }
        return true;
    }

    return false;
}


float QParticle::FastVelocityLength() const
{
    return qSqrt((Velocity.x()*Velocity.x()) + (Velocity.y()*Velocity.y()));
}
